package view;

import java.awt.*;
import java.io.IOException;
import java.util.List;
import javax.swing.*;

import data.Data;
import data.RecordOrShowRecord;

import static java.util.Collections.min;

public class ShowRecord extends JDialog {
    List<Data> record;
    JTextArea showMess;
    RecordOrShowRecord rd;//负责查询数据库的对象

    public ShowRecord() throws IOException {
        rd = new RecordOrShowRecord();
        showMess = new JTextArea();
        showMess.setFont(new Font("楷体", Font.BOLD, 15));
        add(new JScrollPane(showMess));
        setTitle("显示英雄榜");
        setBounds(400, 200, 400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void setGrade(String grade) {
        rd.setTable(grade);
    }

    public void setRecord(List<Data> record) {
        this.record = record;
    }

    public void showRecord() throws IOException {
        showMess.setText(null);
        record = rd.queryRecord();
        if (record.size() == 0) {
            JOptionPane.showMessageDialog
                    (null, "没人上榜呢", "消息框", JOptionPane.WARNING_MESSAGE);
        } else {
            for (int i = 0; i < record.size(); i++) {
                int m = i + 1;
                showMess.append("\n英雄" + m + ":" + record.get(i).getName() + "         " + "成绩：" + record.get(i).getTime());
                showMess.append("\n--------------------------------");
            }
            setVisible(true);
        }
    }
}
